﻿using System;
using System.Data.SqlClient;
using Devv.Core.UniDataTier;

namespace Yglu.BusinessLayer.Entities
{
	public class Combo : BaseEntity
	{
		#region Properties

		public String Title { get; set; }

		public ProductCollection Products { get; set; }
		public Promotion PromotionApplied { get; set; }

		#endregion

		#region Extra properties

		public Double TotalPrice
		{
			get
			{
				Double price = 0;

				foreach (Product p in Products)
				{
					price += p.Price;
				}

				return price;
			}
		}

		public Double PriceWithDiscount
		{
			get
			{
				if (PromotionApplied != null)
				{
					if (PromotionApplied.IsPercent)
					{
						return TotalPrice - (TotalPrice * (PromotionApplied.Discount / 100));
					}

					return TotalPrice - PromotionApplied.Discount;
				}

				return TotalPrice;
			}
		}

		public String Summary
		{
			get
			{
				String s = String.Empty;

				foreach (Product p in Products)
				{
					s += p.Title + " + ";
				}

				return s.Substring(0, s.Length - 3) + " (" + TotalPrice.ToString("0.00") + ")";
			}
		}

		#endregion

		#region Constructor

		public Combo()
		{
			Products = new ProductCollection();
		}

		public Combo(Int32 id)
		{
			Construct(DataAccess.Product_Get(id));
		}

		public Combo(SqlDataReader reader)
		{
			Construct(reader);
		}

		#endregion

		#region Methods

		public override void Load(SqlDataReader reader)
		{
			Id = DataHandler.GetInt32(reader, "ComboId");
			Title = DataHandler.GetString(reader, "ComboTitle");
			Active = DataHandler.GetBoolean(reader, "ComboActive");

			Products = new ProductCollection();
		}

		#endregion
	}
}